/**
 * 验证码业务相关
 */
(function (W, undefined) {
    W.Vcode = {
        params: {
            countdown: 60,
            tempCountdown: 60,
            vCode: null || '#verify_code',
            vPhone: null || '#phone',
            vImgCode: null || '#imgCode',
            vGetCodeBtn: null || '#code',
            vCodeImg: null || '#code_img',
            vImgContent: null || '#img_code_li',
            vLayer: null,
            vMemberType: null,
            vRoleName: null
        },

        checkCode: function (cb) {
            var that = this;
            var url = window.serverUrl + "/verifyCode/verifyCode/verify.do";

            function callBack(ret, err) {
                if (ret.success) {
                    typeof cb === 'function' && cb();
                } else {
                    that.toastMsg('验证码错误', 5);
                }
            }

            var params = {
                phone: $(that.params.vPhone).val(),
                verifyCode: $(that.params.vCode).val()
            };

            $.get(url, params, function (data) {
                callBack(data);
            });
        },
        setCountdown: function ($ae) {
            var that = this;
            if (that.params.tempCountdown == 0) {
                $(that.params.vGetCodeBtn).removeClass('disabled-btn');
                $(that.params.vGetCodeBtn).on('click', function (e) {
                    that.getCheckCode($(this));
                });
                $ae.html('获取验证码');
                that.params.tempCountdown = that.params.countdown;
            } else {
                $(that.params.vGetCodeBtn).addClass('disabled-btn');
                $(that.params.vGetCodeBtn).off('click');
                $ae.html((that.params.tempCountdown--) + 's');
                setTimeout(function () {
                    that.setCountdown($ae)
                }, 1000)
            }
        },
        getCheckCode: function ($ae) {
            var that = this;
            var phoneNum = $(that.params.vPhone).val();
            var isVerifyphone = $(that.params.vPhone).attr('is_verify_phone');
            var isRegister = $(that.params.vPhone).attr('is_register');
            var imgCode = $(that.params.vImgCode).val();


            if (!util.isValid(phoneNum)) {
                that._errFocus(that.params.vPhone, '手机号码不能为空');
                return;
            }
            if (!that._checkPhone(phoneNum)) {
                that._errFocus(that.params.vPhone, '请输入正确的手机号码');
                return;
            }
            if (!util.isValid(imgCode)) {
                that._errFocus(that.params.vImgCode, '请输入右侧图片内容');
                return;
            }
            if (imgCode.length > 10) {
                that._errFocus(that.params.vImgCode, '请输入正确的验证码');
                return;
            }
            if (isVerifyphone=='true') {
                var blo = false;
                var str = "";
                var url = window.serverUrl + "/system/member/checkUniqueProperty.do";
                if (that.params.vMemberType && that.params.vMemberType == 2) {
                    url = window.serverUrl + "/member/group/checkUniqueProperty.do";
                }

                $.ajax({
                    url: url,
                    type: 'get',
                    async: false, // 使用同步的方法
                    dataType: 'json',
                    data: {
                        value: phoneNum,
                        userId: '',
                        key: 'mobile',
                        type: that.params.vMemberType,
                        roleName: that.params.vRoleName
                    },
                    success: function (res) {
                        var isRepeat = !res.data;
                        //res：  存在：false ,不存在：true
                        if (undefined == isRepeat) {
                            str = "验证手机号异常";
                            blo = true;
                        } else if (!isRepeat && isRegister == 'false') {
                            str = "手机号码不存在";
                            blo = true;
                        } else if (isRepeat && isRegister == 'true') {
                            str = "手机号码已存在";
                            blo = true;
                        }
                    }
                })
                if (blo) {
                    that._errFocus(that.params.vPhone, str);
                    return;
                }
            }
            that.setCountdown($ae);

            function callBack(ret, err) {
                if (!ret.success) {
                    that.toastMsg(ret.description, 5);
                    that.params.tempCountdown = 0;
                }
            }

            var url = window.serverUrl + "/verifyCode/verifyCode/mobile/getCode.do";
            var params = {
                imgCode: imgCode,
                phone: phoneNum
            };
            $.post(url, params, function (data) {
                callBack(data);
            });
        },
        _errFocus: function (formInput, msg) {
            var that = this;
            $(formInput).addClass('layui-form-danger');
            $(formInput).focus();
            that.toastMsg(msg, 5);
        },
        _checkPhone: function (phoneNum) {
            if (phoneNum.match(/^1[3-9][0-9]{9}$/)) {
                return true;
            } else {
                return false;
            }
        },
        _getImgCode: function (phoneNum) {
            var that = this;
            if (that._checkPhone(phoneNum)) {
                $(that.params.vCodeImg).attr('src', window.serverUrl + '/verification/mobile/crimg/' + phoneNum + '.do');
                $(that.params.vCodeImg).css("border", "1px solid #e6e6e6");
                $(that.params.vImgContent).show();
                return true;
            } else {
                return false;
            }
        },
        addEvent: function () {
            var that = this;
            $(that.params.vCodeImg).click(function () {
                if (!that._getImgCode($(that.params.vPhone).val())) {
                    that._errFocus(that.params.vPhone, '请输入正确的手机号码');
                }
            });
            $(that.params.vGetCodeBtn).on('click', function (e) {
                if ($(this).hasClass('disabled-btn')) {
                    that.toastMsg($(that.params.vImgCode).attr('placeholder'), 5);
                    return;
                }
                that.getCheckCode($(this));
            });
            $(that.params.vImgCode).on('input propertychange', function (e) {
                if ($(this).val().length == 4) {
                    $(that.params.vGetCodeBtn).removeClass('disabled-btn');
                } else {
                    $(that.params.vGetCodeBtn).addClass('disabled-btn');
                }
            });
            $(that.params.vPhone).on('input propertychange', function (e) {
                that._getImgCode($(this).val());
            });
        },
        unBind: function () {
            var that = this;
            $(that.params.vGetCodeBtn).off('click');
            $(that.params.vPhone).off('input propertychange');
            $(that.params.vImgCode).off('input propertychange');
        },
        loadLayer: function () {
            layui.use('layer', function () {
                vLayer = layui.layer;
            });
        },
        toastMsg: function (msg, icon) {
            vLayer.msg(msg, {icon: icon})
        }
    };

    W.Vcode.loadLayer();
})(window);
